#include "globalsignalmgr.h"

GlobalSignalMgr::GlobalSignalMgr(QObject* parent):QObject(parent){

}

GlobalSignalMgr::~GlobalSignalMgr()
{

}

GlobalSignalMgr *GlobalSignalMgr::get_instance()
{
    if (m_instance_ptr==nullptr){
        QMutex mtex;
        mtex.lock();
        if (m_instance_ptr == nullptr){
            m_instance_ptr = new GlobalSignalMgr;
        }
        mtex.unlock();

    }
    return m_instance_ptr;

}

void GlobalSignalMgr::attach(const QString &type, QObject *receiver, const char *method)
{
    obSrverApater* oa = new obSrverApater();
    if (type.startsWith("STRING")){
        qInfo(QString("bind sig onece %1").arg(type).toStdString().c_str());
        connect(oa, SIGNAL(notify(QString)), receiver, method);
    }
    else if (type.startsWith("VOID")){
        connect(oa, SIGNAL(notify()), receiver, method);
    }
    else{
        throw -1;
    }
    relationData* data = new relationData();
    data ->type = type;
    data->receiver = receiver;
    data->ob = oa;
    m_oRelationList.append(data);
}

void GlobalSignalMgr::detach(const QString &type, const QObject *receiver)
{

}

void GlobalSignalMgr::notify(const QString &type, QString data)
{
    QList<relationData*>::iterator iter = m_oRelationList.begin();
    while(iter != m_oRelationList.end()){
        if((*iter)->type.compare(type) ==0 && type.startsWith("STRING")){
            qInfo("touch onece");
            emit(*iter)->ob->notify(data);
        }
        iter++;
    }
}

void GlobalSignalMgr::notify(const QString &type)
{
    QList<relationData*>::iterator iter = m_oRelationList.begin();
    while(iter != m_oRelationList.end()){
        if((*iter)->type.compare(type) ==0 && type.startsWith("VOID")){
            qInfo("touch onece");
            emit(*iter)->ob->notify();
        }
        iter++;
    }
}


obSrverApater::obSrverApater()
{

}
